package indi.zn.commons.mail;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SimpleMail {
	
	private Properties prop;
	
	public SimpleMail(String path) throws IOException{
	
		prop=MailPropertiesFactory.getMailPropertiesFromFile(path, true);
		
	}
	
	public SimpleMail(Properties prop) throws IOException{
		
		this.prop=prop;
		
	}
	
	public SimpleMail(String path,boolean global) throws IOException{
		
		prop=MailPropertiesFactory.getMailPropertiesFromFile(path, global);
		
	}
	
	
	
    public void sendEmail(String address,String topical,String message,String contentType) throws Exception{
		
		
		Session session=Session.getInstance(prop);
	
		
		
		MimeMessage msg=new MimeMessage(session);
		
		if(contentType!=null){
		msg.setContent(message, contentType);
		}else{
		  msg.setText(message);
		}
		Address ad=new InternetAddress(address);
		
		msg.setFrom(new InternetAddress(prop.getProperty("from")));
		
     msg.setRecipient(Message.RecipientType.TO,ad);
     msg.setSubject(topical);
     msg.setSentDate(new Date());
   
     
     Transport.send(msg, prop.getProperty("username"),prop.getProperty("password"));
     
	}
	
	
	

}
